package com.zbkj.admin.boot.servlet.config;

import net.dreamlu.mica.core.convert.EnumToStringConverter;
import net.dreamlu.mica.core.convert.StringToEnumConverter;
import net.dreamlu.mica.core.utils.DatePattern;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.datetime.standard.DateTimeFormatterRegistrar;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * mica enum 《-》 String 转换配置
 *
 * @author L.cm
 */
@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
public class MicaConverterConfiguration implements WebMvcConfigurer {

	@Override
	public void addFormatters(FormatterRegistry registry) {
		// 注册枚举转换，规则同 jackson
		registry.addConverter(new EnumToStringConverter());
		registry.addConverter(new StringToEnumConverter());
		// 注册时间的转换
		DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
		registrar.setTimeFormatter(DatePattern.NORM_TIME_FORMAT);
		registrar.setDateFormatter(DatePattern.NORM_DATE_FORMAT);
		registrar.setDateTimeFormatter(DatePattern.NORM_DATETIME_FORMAT);
		registrar.registerFormatters(registry);
	}

}
